<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<script>
  function promiseCallback(promise, callback, thisArg) {
    if(callback) {
      function callback2(error, result) {
        try {
          return callback.call(thisArg, error, result);
        } catch(exception) {
          // Show the exception in the console with its full stack trace
          console.trace(exception);
          throw exception;
        }
      }

      promise = promise.then(callback2.bind(undefined, null), callback2);
    }

    return promise
  }

  const objectSelf = {
    test: (promise, callback) => {
      return promiseCallback(promise, callback, objectSelf)
    }
  }

  function testPromise (arg) {
    console.log('testPromise', arg)
    return new Promise((resolve) => resolve(true))
  }

  const test = (promise, callback) => {
    return promiseCallback(promise, callback, this)
  }

  test(testPromise(), function (arg) {
    console.log('callback', arg, this)
  })

  test(testPromise()).then(res => {
    console.log('promise')
  })

  objectSelf.test(testPromise(), function (arg) {
    console.log('callback', arg, this)
  })

  async function testAwait () {
    const data = await test(testPromise(), (error, arg) => arg)
    console.log(data);
  }

  testAwait()

  class TestAsync {
    constructor() {
      this.#init()
    }

    async #init () {
      setTimeout(() => {
        this.obj = 'obj'
      }, 1000)
    }
  }

  const testAsync1 = new TestAsync()

  console.log(testAsync1.obj)
</script>
</body>
</html>
